<template>
  <div class="app-container">
    <el-form :inline="true" label-width="80px">
      <el-form-item label="姓名" prop="realname">
        <el-input
          v-model="queryParams.realname"
          placeholder="请输入姓名"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>

      <el-form-item label="状态">
        <el-select @keyup.enter.native="handleQuery" v-model="queryParams.status" placeholder="不限"
                   clearable
                   size="small">
          <el-option
            v-for="dict in statusOptions"
            :key="dict.dictValue"
            :label="dict.dictLabel"
            :value="dict.dictValue"
          />
        </el-select>
      </el-form-item>

      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button type="primary" icon="el-icon-plus" size="mini" @click="handleAdd"
                   v-hasPermi="['employee:employee:add']">新增
        </el-button>
      </el-form-item>
    </el-form>

    <el-table v-loading="loading" :data="employeeList">
      <el-table-column label="编号" align="center" prop="idNo"/>
      <el-table-column label="真实姓名" align="center" prop="realname"/>
      <el-table-column label="入职日期" align="center" prop="hiredate">
        <template slot-scope="scope">
          {{scope.row.hiredate|dateFilter('YYYY-MM-DD')}}
        </template>
      </el-table-column>
      <el-table-column label="邮箱" align="center" prop="email"/>
      <el-table-column label="状态" align="center" prop="status" :formatter="typeFormat"/>
      <el-table-column label="邮件推送" align="center" prop="isEmail" :formatter="typeFormat"/>
      <el-table-column label="创建人" align="center" prop="creater"/>
      <el-table-column label="创建时间" align="center" prop="createTime" width="180"/>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['employee:employee:edit']"
          >修改
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />
    <!-- 添加或修改员工信息对话框 -->

    <el-dialog :title="title" :visible.sync="open" width="500px" 　:close-on-click-modal="false">
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">

        <el-form-item label="员工编号" prop="idNo">
          <el-input v-model="form.idNo" placeholder="请输入员工编号"/>
        </el-form-item>

        <el-form-item label="手机号码" prop="mobile" v-if="!form.id">
          <el-input v-model="form.mobile" placeholder="请输入手机号码"/>
        </el-form-item>

        <el-form-item label="真实姓名" prop="realname">
          <el-input v-model="form.realname" placeholder="请输入员工真实姓名"/>
        </el-form-item>

        <el-form-item label="邮箱" prop="email">
          <el-input v-model="form.email" placeholder="请输入员工邮箱"/>
        </el-form-item>

        <el-form-item label="入职时间" prop="hiredate">
          <el-date-picker clearable size="small" style="width: 200px"
                          v-model="form.hiredate"
                          type="date"
                          value-format="yyyy-MM-dd"
                          placeholder="请选择入职时间">
          </el-date-picker>
        </el-form-item>

        <el-form-item label="帐号状态" v-if="form.id">
          <el-radio-group v-model="form.status">
            <el-radio :label="item.dictValue" border v-for="item in statusOptions">{{item.dictLabel}}</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="邮件推送">
          <el-radio-group v-model="form.isEmail">
            <el-radio :label="item.dictValue" border v-for="item in emailOptions">{{item.dictLabel}}</el-radio>
          </el-radio-group>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" :disabled="disabled" @click.native.prevent="submitForm">
          <span v-if="!disabled">提 交</span>
          <span v-else>提 交 中...</span>
        </el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>

  </div>
</template>

<script>
  import {
    listEmployee,
    getEmployee,
    delEmployee,
    addEmployee,
    updateEmployee,
    exportEmployee
  } from "@/api/employee/index";

  export default {
    data() {
      return {
        disabled: false,
        // 遮罩层
        loading: true,
        // 总条数
        total: 0,
        // 员工信息表格数据
        employeeList: [],
        // 弹出层标题
        title: "",
        // 是否显示弹出层
        open: false,
        // 查询参数
        queryParams: {
          pageNum: 1,
          pageSize: 10,
          realname: undefined,
          status: undefined
        },
        statusOptions: [{dictValue: '0', dictLabel: '启用'}, {dictValue: '1', dictLabel: '禁用'}],
        emailOptions: [{dictValue: '0', dictLabel: '启用'}, {dictValue: '1', dictLabel: '禁用'}],
        // 表单参数
        form: {
          status: '0',
          isEmail: '0'
        },
        // 表单校验
        rules: {
          realname: [
            {required: true, message: "真实姓名不能为空", trigger: "blur"},
            {max: 30, message: '真实姓名不能超过30个字符', trigger: 'blur'}
          ],
          email: [
            {required: true, message: "邮箱不能为空", trigger: "blur"},
            {max: 50, message: '邮箱不能超过50个字符', trigger: 'blur'},
            {
              type: "email",
              message: "请输入正确的邮箱地址",
              trigger: ["blur", "change"]
            }
          ],
          idNo: [
            {required: true, message: "员工编号不能为空", trigger: "blur"},
            {max: 20, message: '员工编号不能超过20个字符', trigger: 'blur'}
          ],
          mobile: [
            {required: true, message: "手机号码不能为空", trigger: "blur"},
            {
              pattern: /^1[3|4|5|6|7|8|9][0-9]\d{8}$/,
              message: "请输入正确的手机号码",
              trigger: "blur"
            }
          ]
        }
      };
    },
    created() {
      this.getList();
    },
    methods: {
      /** 查询员工信息列表 */
      getList() {
        this.loading = true;
        listEmployee(this.queryParams).then(response => {
          this.employeeList = response.rows;
          this.total = response.total;
          this.loading = false;
        });
      },
      // 取消按钮
      cancel() {
        this.open = false;
        this.reset();
      },
      // 表单重置
      reset() {
        this.disabled = false;
        this.form = {
          id: undefined,
          userId: undefined,
          idNo: undefined,
          realname: undefined,
          hiredate: undefined,
          email: undefined,
          status: "0",
          isEmail: "0",
          creater: undefined,
          createTime: undefined,
          updater: undefined,
          updateTime: undefined
        };
        this.resetForm("form");
      },
      /** 搜索按钮操作 */
      handleQuery() {
        this.queryParams.pageNum = 1;
        this.getList();
      },
      /** 修改按钮操作 */
      handleUpdate(row) {
        this.reset();
        this.form = {
          id: row.id,
          idNo: row.idNo,
          realname: row.realname,
          hiredate: row.hiredate,
          email: row.email,
          status: row.status,
          isEmail: row.isEmail
        };
        this.title = '编辑';
        this.open = true;
      },
      handleAdd() {
        this.reset();
        this.title = '新增';
        this.open = true;
      },
      typeFormat(row, column) {
        let option;
        if (column.property === 'status') {
          option = this.statusOptions;
        } else if (column.property === 'isEmail') {
          option = this.emailOptions;
        }
        return this.selectDictLabel(option, row[column.property]);
      },
      /** 提交按钮 */
      submitForm: function () {
        this.$refs["form"].validate(valid => {
          if (valid) {
            this.disabled = true;
            if (this.form.id) {
              updateEmployee(this.form).then(response => {
                if (response.code === 200) {
                  this.msgSuccess("修改成功");
                  this.open = false;
                  this.getList();
                } else {
                  this.msgError(response.msg);
                }
              });
            } else {
              addEmployee(this.form).then(response => {
                if (response.code === 200) {
                  this.msgSuccess("新增成功");
                  this.open = false;
                  this.getList();
                } else {
                  this.msgError(response.msg);
                }
              }).catch(err => {
                this.disabled = false;
              });
            }
          }
        });
      },
    }
  }
</script>

<style scoped>

</style>
